package app.util.image.action
{
	import app.util.*;
	
	import flash.display.*;
	import flash.filters.*;
	import flash.geom.*;
	
	public class PrintAction extends AbstractAction
	{
		private var fade:Number;
		private var color:uint;
		
		public function setValue(fade:Number, color:uint):PrintAction
		{
			this.fade = fade;
			this.color = color;
			return this;
		}
		
		public override function getDescription():String
		{
			var colorString:String = this.color.toString(16).toUpperCase();
			while(colorString.length < 6)
			{
				colorString = "0" + colorString;
			}
			return "Color Print (" + this.fade.toFixed(0) + ", #" + colorString + ")";
		}
		
		public override function getActionedBitmapData():BitmapData
		{   
			var old:BitmapData = Mediator.getCurrentBitmapData();
			var sobelRet:BitmapData = EdgeDetector.getSobelEdge(old, true);
			var duoColorMatrix:Array = ColorMatrix.IDENTITY.concat();
   			
            duoColorMatrix[4] = 0xff;
            duoColorMatrix[0] = ((this.color >> 16 & 255) - 0xff) / 255;
            duoColorMatrix[9] = 0xff;
            duoColorMatrix[6] = ((this.color >> 8 & 255) - 0xff) / 255;
            duoColorMatrix[14] = 0xff;
            duoColorMatrix[12] = ((this.color >> 0 & 255) - 0xff) / 255;
            duoColorMatrix[18] = 1;		

			sobelRet.applyFilter(sobelRet, sobelRet.rect, new Point(0, 0), 
				new ColorMatrixFilter(duoColorMatrix));			
			sobelRet.merge(old, sobelRet.rect, new Point(0, 0), 256 - fade,
				256 - fade, 256 - fade, 256 - fade);
			return sobelRet;
		}		
	}
}